import { IBreadcrumb } from '@/base-ui/breadcrumb/types'
import { RouteRecordRaw } from 'vue-router'

let firstMenu: any = null

export function mapMenusToRoutes(userMenus: any[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []

  // 1.先加载默认的所有的routes
  //   找到所有router.main文件下的所有的注册好的路由route,添加到allRoutes里
  const allRoutes: RouteRecordRaw[] = []
  // 第二个参数： true代表是否迭代查找
  const routeFiles = require.context('../router/main', true, /\.ts/)
  routeFiles.keys().forEach((key) => {
    const route = require('../router/main' + key.split('.')[1])
    allRoutes.push(route.default)
  })

  // 2.根据角色权限所拥有的菜单,获取添加对应的routes
  // userMenus:
  // type === 1 -> children -> type === 2
  // type === 2 -> url -> route
  const _recurseGetRoute = (userMenus: any[]) => {
    for (const menu of userMenus) {
      if (menu.type === 2) {
        // 找到allRoutes里的url与userMenus中的path一致的route
        const route = allRoutes.find((route) => route.path === menu.url)
        // 添加到routes数组里
        if (route) routes.push(route)
        // 保存第一个遍历出来的menu
        if (!firstMenu) {
          firstMenu = menu.url
        }
      } else {
        // 如果type=1,递归调用
        _recurseGetRoute(menu.children)
      }
    }
  }

  _recurseGetRoute(userMenus)

  return routes
}

export { firstMenu }

// 3.找出breadcrumb需要的name和path
export function pathMapBreadcrumb(userMenus: any[], currentPath: string) {
  const breadcrumbs: IBreadcrumb[] = []

  pathMapToMenu(userMenus, currentPath, breadcrumbs)

  return breadcrumbs
}

// 4.根据path找出menu
// 当userMenus里的url与route里的当前path(currentPath)对应的时候，将其添加到数组里，将其展示
export function pathMapToMenu(
  userMenus: any[],
  currentPath: string,
  breadcrumbs?: IBreadcrumb[]
): any {
  for (const menu of userMenus) {
    if (menu.type === 1) {
      const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
      if (findMenu) {
        // 将type = 1和type = 2的name和path push到breadcrumbs里
        breadcrumbs?.push({ name: menu.name })
        breadcrumbs?.push({ name: findMenu.name })
        return findMenu
      }
    } else if (menu.type === 2 && menu.url === currentPath) {
      return menu
    }
  }
}

// // 找出breadcrumb需要的name和path
// export function pathMapBreadcrumb(userMenus: any[], currentPath: string) {
//   const breadcrumbs: IBreadcrumb[] = []
//   for (const menu of userMenus) {
//     if (menu.type === 1) {
//       const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
//       if (findMenu) {
//         // 将type = 1和type = 2的name和path push到breadcrumbs里
//         breadcrumbs.push({ name: menu.name, path: menu.url })
//         breadcrumbs.push({ name: findMenu.name, path: findMenu.url })
//         return findMenu
//       }
//     } else if (menu.type === 2 && menu.url === currentPath) {
//       return menu
//     }
//   }
//   return breadcrumbs
// }

// // 根据path找出menu
// export function pathMapToMenu(userMenus: any[], currentPath: string): any {
//   for (const menu of userMenus) {
//     if (menu.type === 1) {
//       const findMenu = pathMapToMenu(menu.children ?? [], currentPath)
//       if (findMenu) {
//         return findMenu
//       }
//     } else if (menu.type === 2 && menu.url === currentPath) {
//       return menu
//     }
//   }
// }

// 5.获取type=3的菜单权限
export function mapMenusToPermission(userMenus: any[]) {
  const permission: string[] = []

  const _recurseGetMenus = (userMenus: any[]) => {
    for (const item of userMenus) {
      if (item.type === 1 || item.type === 2) {
        _recurseGetMenus(item.children ?? [])
      } else if (item.type === 3) {
        permission.push(item.permission)
      }
    }
  }

  _recurseGetMenus(userMenus)

  return permission
}

// 6.将菜单的每个节点都转为叶子节点
export function mapMenusToLeafNodes(menuList: any[]) {
  const leafKeys: number[] = []

  const _recurseGetId = (menuList: any[]) => {
    for (const item of menuList) {
      if (item.children) {
        _recurseGetId(item.children)
      } else {
        leafKeys.push(item.id)
      }
    }
  }

  _recurseGetId(menuList)

  return leafKeys
}
